package jdbctest;

import java.sql.*;

/**
 * Created by Administrator on 2017/9/11.
 */
public class ConnectionTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3307/world","root","123456");
        if(conn!=null){
            System.out.println("ok");
        }else {
            System.out.println("error");
        }
        //操作数据Connection不能直接操作数据库
        Statement statement=conn.createStatement();
        //查询语句
        String sql="select *from city";
        ResultSet set =statement.executeQuery(sql);
        while (set.next()){
            //System.out.println(set.getString(1)+","+set.getString(2)+","+set.getString(3));
            User user =new User();
            user.setId(set.getInt("ID"));
            user.setName(set.getString(2));
            System.out.println(user);
        }
        set.close();
        statement.close();
        conn.close();
    }
}
